//
//  YZWaterfallCollectionVC.m
//  CollectionTest
//
//  Created by yuze on 2016/10/20.
//  Copyright © 2016年 yuze. All rights reserved.
//

#import "YZWaterfallCollectionVC.h"
#import "YZCollectionViewWaterfallLayout.h"

#define RGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define RGBAColor(r, g, b ,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
#define RandColor RGBColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))

@interface YZWaterfallCollectionVC () <UICollectionViewDelegate, UICollectionViewDataSource, WaterfallLayoutDelegate>

@property(nonatomic, strong) NSArray *cellHeightArray;

@end

static NSString *CollectionCellReuseId = @"collectionCelllIdTest";

@implementation YZWaterfallCollectionVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"瀑布式布局";
    
    self.cellHeightArray = @[@120, @136, @160, @36, @60, @90];
    /// 创建布局样式 基类：UICollectionViewLayout 子类：YZCollectionViewWaterfallLayout
    ///
    YZCollectionViewWaterfallLayout *waterfallLayout = [[YZCollectionViewWaterfallLayout alloc] init];
    waterfallLayout.delegate = self;
    /// UICollectionView 实例化
    ///
    CGRect collectionViewRect = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:collectionViewRect collectionViewLayout:waterfallLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    
    /// 注册Cell类
    ///
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionCellReuseId];
    
    [collectionView reloadData];
}

#pragma mark -- WaterfallLayoutDelegate

- (CGFloat)waterfallLayout:(YZCollectionViewWaterfallLayout *)waterFlowLayout heightForRowAtIndex:(NSInteger)index itemWidth:(CGFloat)width
{
    return [self.cellHeightArray[arc4random_uniform(6)] doubleValue];
}

//决定cell的列数
- (NSInteger)cloumnCountInWaterfallLayout:(YZCollectionViewWaterfallLayout *)waterFlowLayout
{
    return 4;
}

//决定cell 的列的距离
- (CGFloat)columMarginInWaterfallLayout:(YZCollectionViewWaterfallLayout *)waterFlowLayout
{
    return 4.0;
}

//决定cell 的行的距离
- (CGFloat)rowMarginInWaterfallLayout:(YZCollectionViewWaterfallLayout *)waterFlowLayout
{
    return 5.0;
}

//决定cell 的边缘距
- (UIEdgeInsets)edgeInsetInWaterfallLayout:(YZCollectionViewWaterfallLayout *)waterFlowLayout
{
    return UIEdgeInsetsMake(6, 6, 6, 6);
}

#pragma mark -- UICollectionViewDataSource

/// 分为几部分
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

/// 多少个单元格
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 66;
}

/// 获取注册cell item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionCellReuseId forIndexPath:indexPath];
    
    cell.contentView.backgroundColor = RandColor;
    return cell;
}

#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell= [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = RandColor;
}

@end

